package com.ukao.cashregister.base;


import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.ukao.cashregister.R;
import com.ukao.cashregister.retrofit.ApiClient;
import com.ukao.cashregister.retrofit.ApiStores;
import com.ukao.cashregister.ui.MainActivity;
import com.ukao.cashregister.ui.login.LoginActivity;
import com.ukao.cashregister.ui.splash.SplashActivity;
import com.ukao.cashregister.utils.AppManager;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.SystemUIUtils;
import com.ukao.cashregister.widget.ZProgressHUD;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import cn.jiguang.analytics.android.api.JAnalyticsInterface;
import me.yokeyword.fragmentation.SupportActivity;
import retrofit2.Call;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

import static android.view.View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
import static com.ukao.cashregister.utils.SystemUIUtils.setStickFullScreen;

/**
 * Created by ukaodev on 2017/5/24.
 */

public abstract class BaseActivity extends SupportActivity {

    private List<Call> calls;
    private CompositeSubscription mCompositeSubscription;
    private ZProgressHUD progressDialog;
    protected static final String ARG_PARAM1 = "param1";
    protected static final String ARG_PARAM2 = "param2";
    protected static final String ARG_PARAM3 = "param3";
    protected static final String ARG_PARAM4= "param4";
    protected static final String ARG_PARAM5= "param5";
    protected  boolean useEventBus(){
        return false;
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        init();
        AppManager.getAppManager().addActivity(this);
        if(useEventBus())
        EventBus.getDefault().register(this);//订阅
        super.onCreate(savedInstanceState);
        setTheme(R.style.BlueTheme);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);


    }

    protected  void init(){
        SystemUIUtils.setStickFullScreen( getWindow());

    };


    @Override
    protected void onResume() {
        super.onResume();
        JAnalyticsInterface.onPageStart(getApplicationContext(),this.getClass().getCanonicalName());
    }


    @Override
    protected void onPause() {
        super.onPause();
        JAnalyticsInterface.onPageEnd(getApplicationContext(),this.getClass().getCanonicalName());
    }



    protected abstract void initView();


    protected void initListener() {
    }

    public ApiStores apiStores() {
        return ApiClient.retrofit().create(ApiStores.class);
    }


    public void onUnsubscribe() {
        //取消注册，以避免内存泄露
        if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions())
            mCompositeSubscription.unsubscribe();
    }


    public void addSubscription(Observable observable, Subscriber subscriber) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        mCompositeSubscription.add(observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber));
    }




    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        if (intent != null && intent.getComponent() != null&& !intent.getComponent().getClassName().equals(SplashActivity.class.getName())) {
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        }
    }
    @Override
    public void finish() {
        super.finish();
        if (!((Object) this).getClass().equals(MainActivity.class) && !((Object) this).getClass().equals(LoginActivity.class)) {
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        }
    }

    public ZProgressHUD showProgressDialog(String message) {
        progressDialog= ZProgressHUD.getInstance(this);
        progressDialog.setMessage(message);
        progressDialog.show();
        return progressDialog;
    }

    public ZProgressHUD showProgressDialog( ) {
        if (progressDialog==null){
            progressDialog = new ZProgressHUD(this);
        }
        progressDialog.show();
        return progressDialog;
    }



    public void dismissProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            // progressDialog.hide();会导致android.view.WindowLeaked
            progressDialog.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.getAppManager().finishActivity(this);
        onUnsubscribe();
        mCompositeSubscription=null;
        progressDialog=null;
        if(useEventBus())
        EventBus.getDefault().unregister(this);//解除订阅
    }





}
